package com.bamanker.sms_service.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.bamanker.sms_service.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author bamanker
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/doLogin")
    @ResponseBody
    public SaResult doLogin(@RequestBody User user) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("bamanker".equals(user.getUsername()) && "8803101".equals(user.getPassword())) {
            StpUtil.login(10001);
            return SaResult.ok("login success!");
        }
        return SaResult.error("login failed!");
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("/isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

    @RequestMapping("/doLogout")
    public String logout() {
        StpUtil.logout();
        return "login";
    }
}
